<!--
@name:dialogIndicator
@author:Zhenglei
@date:10:14
@description:dialogIndicator
@update:2022/8/2510:14
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :append-to-body="true"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="60%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="类型：" class="is-required">
        <el-select v-model="subData.fromType"
                   class="w-300"
                   :disabled="isEdit"
                   @change="handleChangeType"
                   placeholder="请选择类型">
          <el-option v-for="item in typeDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="指标名称：" prop="name">
        <el-input v-model="subData.name" class="w-300" placeholder="请输入指标名称" clearable/>
      </el-form-item>
      <el-form-item label="单位：">
        <el-input v-model="subData.unit" class="w-300" placeholder="请输入单位" clearable/>
      </el-form-item>
      <el-form-item label="编码：" prop="code">
        <el-input v-model="subData.code" class="w-300" placeholder="请输入编码" clearable/>
      </el-form-item>
      <!--      设备检测-->
      <el-form-item v-if="subData.fromType===2" label="检测指标：" prop="formula.deviceParams">
        <el-select v-model="subData.formula.deviceParams"
                   class="w-300"
                   multiple
                   filterable
                   clearable
                   placeholder="请选择检测指标">
          <el-option v-for="item in allTargetDataSource"
                     :key="item.code"
                     :label="item.name"
                     :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      计算指标-->
      <div v-if="subData.fromType===3">
        <el-form-item label="关联指标：">
          <el-select v-model="subData.childIds"
                     class="w-300"
                     multiple
                     filterable
                     clearable
                     placeholder="请选择关联指标">
            <el-option v-for="item in correlationMetricsDataSource"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="条件判断：">
          <el-select v-model="subData.formula.conditionParams"
                     class="w-300"
                     multiple
                     filterable
                     clearable
                     placeholder="请选择条件判断">
            <el-option v-for="item in conditionDataSource"
                       :key="item.code"
                       :label="item.name"
                       :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="检测指标：">
          <el-select v-model="subData.formula.deviceParams"
                     class="w-300"
                     multiple
                     filterable
                     clearable
                     placeholder="请选择检测指标">
            <el-option v-for="item in allTargetDataSource"
                       :key="item.code"
                       :label="item.name"
                       :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数：" v-if="subData.formula">
          <el-checkbox-group v-model="subData.formula.extraParams">
            <el-checkbox label="sex">性别</el-checkbox>
            <el-checkbox label="age">年龄</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="计算公式：" prop="formula.formula">
          <el-input v-model="subData.formula.formula" class="w-300" type="textarea" autosize placeholder="请输入计算公式" clearable/>
        </el-form-item>
        <el-form-item label="特殊模式字段：">
          <el-input v-model="subData.specialModel" type="number" class="w-300" placeholder="请输入特殊模式字段" clearable/>
        </el-form-item>

      </div>
      <el-form-item label="相关知识：">
        <vue-neditor-wrap :destroy="true"
                          v-model="subData.knowledge"
                          :config="myConfig"/>
      </el-form-item>
    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
  import VueNeditorWrap from "vue-neditor-wrap";

  export default {
    name: "dialogIndicator",
    props: {
      dataObj: {
        type: Object,
        default: {}
      },
      visible: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        title: '新 增',
        isEdit: false,

        subData: {
          id: null,
          fromType: 1,//默认手动输入
          name: '',
          unit: '',
          code: '',
          knowledge: '',
          specialModel: '',
          childIds: [],
          formula: {
            conditionParams: [],
            deviceParams: [],
            formula: '',
            extraParams:[],
          }
        },
        typeDataSource: [
          {
            name: '手动输入',
            id: 1
          },
          {
            name: '设备检测',
            id: 2
          },
          {
            name: '计算指标',
            id: 3
          }
        ],
        detectionDataSource: [],
        targetDataSource: [],
        allTargetDataSource: [],
        correlationMetricsDataSource: [],
        conditionDataSource: [],
        myConfig: {
          // 你的UEditor资源存放的路径,相对于打包后的index.html
          UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
          // 编辑器不自动被内容撑高
          autoHeightEnabled: false,
          // 初始容器高度
          initialFrameHeight: '418',
          // 初始容器宽度
          initialFrameWidth: '100%',
          // 关闭自动保存
          enableAutoSave: false,
          scaleEnabled: true
        },
        formModelRules: {
          name: [{required: true, message: '请输入指标名称', trigger: 'blur'},],
          code: [{required: true, message: '请输入编码', trigger: 'blur'},],
          specialModel: [{required: true, message: '请输入特殊字段模式', trigger: 'blur'},],
          knowledge: [{required: true, message: '请输入相关知识', trigger: 'blur'},],
          'formula.formula': [{required: true, message: '请输入公式', trigger: 'blur'},],
          'formula.deviceParams': [{required: true, message: '请选择检测指标', trigger: 'blur'},],
        },
      }
    },
    created() {
      if (this.dataObj.id) {
        this.title = '编 辑'
        this.isEdit = true
        this.getDetail()
      } else {
        this.title = '新 增'
        this.isEdit = false
      }
      this.getHealthMapIndexItemList()
      this.getAllFieldCNName()
      this.getConditionList()
    },
    methods: {
      modalClose() {
        this.$emit('update:visible', false);
      },

      handleChangeType() {
        this.subData.formula.deviceParams = []
      },
      handleSave() {
        this.$refs.formModelRef.validate(valid => {
          if (valid) {
            let dealData = JSON.parse(JSON.stringify(this.subData))
            if (dealData.childIds.length !== 0) {
              dealData.childIds = dealData.childIds.toString()
            } else {
              dealData.childIds = ''
            }
            this.post(this.api.healthMap.saveIndexItem, dealData).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('保存成功')
                this.$parent.getList()
                this.modalClose()
              }
            })
          }
        })
      },


      //查询关联指标
      getHealthMapIndexItemList() {
        this.post(this.api.healthMap.getHealthMapIndexItemList).then((res) => {
          if (res.data.code === '200') {
            this.correlationMetricsDataSource = res.data.object
          }
        })
      },
      //查询所有指标
      getAllFieldCNName() {
        this.post(this.api.getAllFieldCNName).then((res) => {
          if (res.data.code === '200') {
            this.allTargetDataSource = res.data.object
          }
        })
      },
      //查询条件
      getConditionList() {
        this.post(this.api.healthMap.getConditionList).then((res) => {
          if (res.data.code === '200') {
            this.conditionDataSource = res.data.object
          }
        })
      },
      //查询详情
      getDetail() {
        this.post(this.api.healthMap.getIndexItemDetail, {
          id: this.dataObj.id
        }).then((res) => {
          if (res.data.code === '200') {

            this.subData = Object.assign(this.subData, res.data.object)
            if (!res.data.object.formula.extraParams){
              this.$set(this.subData.formula,'extraParams',[])
            }
            if (this.subData.childIds){
              this.subData.childIds = res.data.object.childIds.split(',')
              if (this.subData.childIds.length > 0) {
                this.subData.childIds = this.subData.childIds.map(item => parseInt(item))
              }
            }else {
              this.subData.childIds=[]
            }

          console.log(this.subData)
          }
        })
      },

    },
    components: {
      VueNeditorWrap
    }
  }
</script>

<style scoped>

</style>
